package com.khalil.kkadmin.module.ai.model;

import com.khalil.kkadmin.module.ai.application.domain.AiApplication;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;


public class AiChatModelBuilder {

    public static StreamingChatModel buildStreamingChatModel(AiApplication aiApplication, AiModel aiModel) {
        String modelName = aiModel.getName();
        String apiKey = aiModel.getApiKey();
        String baseUrl = aiModel.getBaseUrl();
        AiModel.Vendor vendor = aiModel.getVendor();

        Float temperature = aiApplication.getTemperature();
        Integer maxTokens = aiApplication.getMaxTokens();

        StreamingChatModel chatModel;
        switch (vendor) {
            case AiModel.Vendor.DASHSCOPE:
                chatModel = QwenStreamingChatModel.builder()
                        .apiKey(apiKey)
                        .modelName(modelName)
                        .temperature(temperature)
                        .maxTokens(maxTokens)
                        .build();
                break;
            case AiModel.Vendor.OPENAI_REST:
                chatModel = OpenAiStreamingChatModel.builder()
                        .baseUrl(baseUrl)
                        .apiKey(apiKey)
                        .modelName(modelName)
                        .temperature(temperature.doubleValue())
                        .logRequests(true)
                        .logResponses(true)
                        .build();
                break;
            default:
                chatModel = null;
                throw new RuntimeException("不支持的模型厂商");
        }
        return chatModel;
    }
}
